﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LIT.Logger.Client;
using LIT.Logger.ServerCheckerBL.Checkers;
using Moq;
using LIT.Logger.ServerCheckerBL;
using System.Collections.Generic;

namespace UnitTest.ServerChecker
{
    [TestClass]
    public class ServiceAndProcessTest
    {
        private EventSenderMock m_eventSender;
        private Mock<IConfiguration> m_config;

        [TestInitialize]
        public void Init()
        {
            m_eventSender = new EventSenderMock();
            m_config = new Mock<IConfiguration>();
        }

        [TestMethod]
        public void ServiceNotExists()
        {
            m_config.Setup(s=>s.CheckServices).Returns(() => { return new List<string>() { "ServiceNameThatDoesNotExist" };});
            ServiceAndProcess test = new ServiceAndProcess(m_eventSender, m_config.Object);
            test.CheckServices();
            Assert.AreEqual(1, m_eventSender.EventList.Count);
            Assert.AreEqual("Service status could not be read.", m_eventSender.EventList[0].Message);
            Assert.AreEqual("Service name: ServiceNameThatDoesNotExist", m_eventSender.EventList[0].Details);
        }

        [TestMethod]
        public void ServiceRunning()
        {
            m_config.Setup(s => s.CheckServices).Returns(() => { return new List<string>() { "Appinfo" }; });
            ServiceAndProcess test = new ServiceAndProcess(m_eventSender, m_config.Object);
            test.CheckServices();
            Assert.AreEqual(0, m_eventSender.EventList.Count);
        }

        [TestMethod]
        public void ServiceNotRunning()
        {
            m_config.Setup(s => s.CheckServices).Returns(() => { return new List<string>() { "Fax" }; });
            ServiceAndProcess test = new ServiceAndProcess(m_eventSender, m_config.Object);
            test.CheckServices();
            Assert.AreEqual(1, m_eventSender.EventList.Count);
            Assert.AreEqual("Service is not running.", m_eventSender.EventList[0].Message);
            Assert.AreEqual("Service name: Fax", m_eventSender.EventList[0].Details);
        }

        [TestMethod]
        public void ProcessRunning()
        {
            m_config.Setup(s => s.CheckProcesses).Returns(() => { return new List<string>() { "devenv" }; });
            ServiceAndProcess test = new ServiceAndProcess(m_eventSender, m_config.Object);
            test.CheckProcesses();
            Assert.AreEqual(0, m_eventSender.EventList.Count);
        }

        [TestMethod]
        public void ProcessNotRunning()
        {
            m_config.Setup(s => s.CheckProcesses).Returns(() => { return new List<string>() { "ProcessNameThatDoesNotExist" }; });
            ServiceAndProcess test = new ServiceAndProcess(m_eventSender, m_config.Object);
            test.CheckProcesses();
            Assert.AreEqual(1, m_eventSender.EventList.Count);
            Assert.AreEqual("Process is not running.", m_eventSender.EventList[0].Message);
            Assert.AreEqual("Process name: ProcessNameThatDoesNotExist", m_eventSender.EventList[0].Details);
        }
    }
}
